package com.survey.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.survey.bean.News;
import com.survey.bean.Ques;
import com.survey.bean.QuesItem;
import com.survey.bean.QuesItemList;
import com.survey.bean.QuesItemListOption;
import com.survey.bean.QuesItemListOptionAnswer;
import com.survey.bean.QuesUser;
import com.survey.bean.SimpleUser;
import com.survey.util.DateUtil;
import com.survey.util.MapUtil;
import com.survey.util.common.BaseAction;
import com.survey.service.NewsService;
import com.survey.service.QuesItemListOptionAnswerService;
import com.survey.service.QuesItemListOptionService;
import com.survey.service.QuesItemListService;
import com.survey.service.QuesItemService;
import com.survey.service.QuesService;
import com.survey.service.QuesUserService;

@Controller
@RequestMapping("/com")
public class WebAction extends BaseAction {

	@RequestMapping(value = "/index.do")
	public String index() {
		List<Ques> selectAll = quesService.selectAll(Ques.class, "canyu desc");
		putRequestValue("list", selectAll);

		return "forward:/qiantai/index.jsp";
	}

	@RequestMapping(value = "/listNews.do")
	public String listNews() {
		putRequestValue("list", newsService.selectAll(News.class, "id desc"));
		return "forward:/qiantai/listNews.jsp";
	}

	@RequestMapping(value = "/getNews.do")
	public String getNews(int uid) {
		putRequestValue("item", newsService.get(uid));
		return "forward:/qiantai/news.jsp";
	}

	//保存投票参与结果
	@RequestMapping(value = "/addQuesUser.do")
	public String addQuesUser(QuesUser bean) {
		try {
			List<QuesItemList> list = new ArrayList<QuesItemList>();
			List<QuesItemListOptionAnswer> anslist = new ArrayList<QuesItemListOptionAnswer>();
			List<QuesItem> ilist = quesItemService.selectAll(QuesItem.class, MapUtil.createMap("quesId", bean.getQuesId()),
					"id");
			JSONArray jsonary = new JSONArray();
			for (QuesItem item : ilist) {
				List<QuesItemList> llist = quesItemListService.selectAll(QuesItemList.class,
						MapUtil.createMap("itemId", item.getId()), "id");
				for (QuesItemList itemList : llist) {
					if (itemList.getTypesel().equals("单选")) {
						String v = request.getParameter("score_" + itemList.getId());
						if (StringUtils.isBlank(v)) {
							putSessionValue("errorMessage", "请先完成投票所有题目.");
							return "redirect:/com/getQues.do?uid=" + bean.getQuesId();
						}
						itemList.setUserchose(v);

						JSONObject obj = new JSONObject();
						obj.put("type", "单选");
						obj.put("text", "");
						JSONArray ary = new JSONArray();
						ary.add(v);
						obj.put("options", ary);
						jsonary.add(obj);
					} else if (itemList.getTypesel().equals("多选")) {
						String[] v = request.getParameterValues("score_" + itemList.getId());
						if (v == null || v.length == 0) {
							putSessionValue("errorMessage", "请先完成投票所有题目.");
							return "redirect:/com/getQues.do?uid=" + bean.getQuesId();
						}
						itemList.setUserchose(v);

						JSONObject obj = new JSONObject();
						obj.put("type", "多选");
						obj.put("text", "");
						JSONArray ary = new JSONArray();
						for (String tempv : v) {
							ary.add(tempv);
						}
						obj.put("options", ary);
						jsonary.add(obj);
					} else if (itemList.getTypesel().equals("填空") || itemList.getTypesel().equals("文本")) {
						String v = request.getParameter("score_" + itemList.getId());
						itemList.setUserchose(v);
						if (StringUtils.isBlank(v)) {
							putSessionValue("errorMessage", "请先完成投票所有题目.");
							return "redirect:/com/getQues.do?uid=" + bean.getQuesId();
						}
						QuesItemListOptionAnswer ans = new QuesItemListOptionAnswer();
						ans.setAnswer(v);
						ans.setItemListId(itemList.getId());
						anslist.add(ans);

						JSONObject obj = new JSONObject();
						obj.put("type", "填空");
						obj.put("text", v);
						obj.put("options", itemList.getId());
						jsonary.add(obj);
					}

				}
				list.addAll(llist);
			}
			bean.setQresult(jsonary.toJSONString());

			bean.setUserId(getSimpleUser().getId());
			bean.setAddDate(DateUtil.getCurrentTime());
			quesUserService.addQuesUser(bean, list, anslist);

			putSessionValue("quesUser", bean);
			//			putSessionValue("errorMessage", "提交成功.");

			Ques q = quesService.get(bean.getQuesId());
			q.setCanyu(q.getCanyu() + 1);
			quesService.update(q);

			return "redirect:/qiantai/quesResult.jsp";
		} catch (Exception e) {
			e.printStackTrace();
			putSessionValue("errorMessage", "提交失败.");
			return "redirect:/com/getQues.do?uid=" + bean.getQuesId();
		}

	}

	@RequestMapping(value = "/getQues.do")
	public String getQues(int uid) {
		if (getWebuser() == null) {
			putSessionValue("errorMessage", "请先登录.");
			return "redirect:/com/index.do";
		}
		Ques temp = quesService.get(uid);
		if (temp.getEndDate().compareTo(DateUtil.getCurrentTime()) < 0) {
			putSessionValue("errorMessage", "投票已经过期.");
			return "redirect:/com/index.do";
		}

		if (!temp.getQstatus().equals("已发布")) {
			putRequestValue("errorMessage", "投票未发布");
			return "getQuesAll";
		}

		if ("否".equals(temp.getChongfu())) {
			List templist = quesUserService.selectAll(QuesUser.class,
					MapUtil.createMap("userId", getSimpleUser().getId(), "quesId", temp.getId()));
			if (templist.size() > 0) {
				putRequestValue("errorMessage", "此投票您已经参与过了.不能重复投票");
				return "redirect:/com/index.do";
			}
		}

		request.setAttribute("modifybean", temp);

		List<QuesItem> ilist = quesItemService.selectAll(QuesItem.class, MapUtil.createMap("quesId", temp.getId()), "id");
		for (QuesItem item : ilist) {
			List<QuesItemList> llist = quesItemListService.selectAll(QuesItemList.class,
					MapUtil.createMap("itemId", item.getId()), "id");
			item.setList(llist);
			for (QuesItemList itemList : llist) {
				List olist = quesItemListOptionService.selectAll(QuesItemListOption.class,
						MapUtil.createMap("quesItemListId", itemList.getId()), "id");
				itemList.setList(olist);
			}
		}

		putRequestValue("list", ilist);

		return "forward:/qiantai/ques.jsp";
	}

	private SimpleUser getWebuser() {
		try {
			return getSimpleUser();
		} catch (Exception e) {
		}
		return null;
	}

	@Autowired
	private QuesService											quesService;
	@Autowired
	private QuesItemService									quesItemService;
	@Autowired
	private QuesItemListService							quesItemListService;
	@Autowired
	private QuesItemListOptionService				quesItemListOptionService;
	@Autowired
	private QuesUserService									quesUserService;
	@Autowired
	private QuesItemListOptionAnswerService	quesItemListOptionAnswerService;
	@Autowired
	private NewsService											newsService;

}
